{% extends 'layout.html' %}
{% load static %}

{% block content %}

<div class="container">
    <div style="margin-bottom: 20px;">
{#        <input type="button" value="新建订单1" class="btn btn-primary" data-toggle="modal" data-target="#myModal">#}
{#        <input id="btnAdd" type="button" value="新建订单2" class="btn btn-primary">#}
         <a class="btn btn-primary" href="/order/add/" target="_blank">新建</a>
    </div>
    <div>
        <div class="panel panel-default">
            <!-- Default panel contents -->
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list" aria-hidden="true" style="margin-right: 5px;"></span>
                <span>订单列表</span>
            </div>

            <!-- Table -->
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>订单号</th>
                        <th>名称</th>
                        <th>价格</th>
                        <th>状态</th>
                        <th>管理员</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for obj in queryset %}
                    <tr>
                        <th>{{ obj.id }}</th>
                        <th>{{ obj.oid }}</th>
                        <th>{{ obj.title }}</th>
                        <td>{{ obj.price }}</td>
                        <td>{{ obj.get_status_display }}</td>
                        <td>{{ obj.admin.username }}</td>
                        <td>
                            <a class="btn btn-primary btn-xs" href="#">编辑</a>
                            <a class="btn btn-danger btn-xs" href="#">删除</a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <ul class="pagination">
        {{ page_string }}
    </ul>
    <br>

    <form method="get">
        <div style="display:inline-block; width: 150px;">
            <div class="input-group">
                <span> <input type="text" class="form-control" placeholder="请输入页码" name="page"></span>
                <span class="input-group-btn">
                    <button class="btn btn-primary" type="submit">跳转</button>
                </span>
            </div>
        </div>
    </form>
</div>

<!-- 新建订单 - 对话框 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">新建订单</h4>
            </div>
            <div class="modal-body">
                <form method="post" id="formSave" novalidate>
                    <div>
                        {% for item in form %}
                        <div class="col-xs-6">
                            <div class="form-group" style="position: relative; margin-top: 5px">
                                <label>{{ item.label }}</label>
                                {{ item }}
                                <span class="error_msg" style="color: red;position: absolute;"></span>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </form>
            </div>
            <div class="modal-footer bottom-right" style="margin-top: 250px;">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="btnSave">保存</button>
            </div>
        </div>
    </div>
</div>

{% endblock %}



{% block script %}

     <script src="{% static "js/jquery-3.7.1.js" %}"></script>
  <script type="text/javascript">
    $(function(){
        bindBtnAddEvent();
    })

    function bindBtnAddEvent(){
        $("#btnAdd").click(function(){
            $("#myModal").modal('show');
        });
    }

    function bindBtnSaveEvent(){
  $("#btnSave").click(function(){
    // 清除错误信息
    $(".error_msg").empty();

    $.ajax({
      url: "/order/add/",
      type: "post",
      data: $("#formSave").serialize(),
      dataType: "JSON",
      success: function(res) {
        if(res.status){
          alert("创建订单成功");
        } else {
          $.each(res.error, function(name, errorlist){
            $("#id_" + name).next().text(errorlist[0]);
          })
        }
      }
    })
  });

  </script>
{% endblock %}
